package com.lin.codesandbox.codesandbox.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.command.LogContainerResultCallback;

import java.util.List;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/9/3 10:30
 */
public class DockerDemo {
    public static void main(String[] args) {
        String image = "sunny158/hello-world";
        DockerClient dockerClient = DockerClientBuilder.getInstance().build();
//        pullImage(image,dockerClient);
        String containerId = createContainer(image,dockerClient);
        checkContainerStatus(image,dockerClient);
    }

    /**
     * 拉取镜像方法
     * @param imageName
     * @param dockerClient
     */
    public static void pullImage(String imageName, DockerClient dockerClient){
        PullImageCmd pullImageCmd = dockerClient.pullImageCmd(imageName);
        PullImageResultCallback pullImageResultCallback = new PullImageResultCallback() {
            @Override
            public void onNext(PullResponseItem item) {
                System.out.println("下载镜像：" + item.getStatus());
                super.onNext(item);
            }
        };
        try {
            pullImageCmd.exec(pullImageResultCallback).awaitCompletion();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("下载完成");
    }

    /**
     * 创建容器
     * @param imageName
     * @param dockerClient
     */
    public static String createContainer(String imageName,DockerClient dockerClient){
        CreateContainerCmd containerCmd = dockerClient.createContainerCmd(imageName);
        CreateContainerResponse createContainerResponse = containerCmd.withCmd("echo", "Hello Docker").exec();
        System.out.println(createContainerResponse);
        return createContainerResponse.getId();
    }

    /**
     * 查看容器状态
     * @param imageName
     * @param dockerClient
     */
    public static void checkContainerStatus(String imageName,DockerClient dockerClient){
        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
        List<Container> containerList = listContainersCmd.withShowAll(true).exec();
        for(Container container : containerList){
            System.out.println(container);
        }
    }

    /**
     * 启动容器，容器id是通过创建容器时得到的response中获取的getId
     * @param containerId
     * @param dockerClient
     */
    public static void startContainer(String containerId,DockerClient dockerClient){
        dockerClient.startContainerCmd(containerId).exec();
    }

    /**
     * 查看日志
     * @param containerId
     * @param dockerClient
     */
    public static void printLogs(String containerId,DockerClient dockerClient){
        LogContainerResultCallback logContainerResultCallback = new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                System.out.println(item.getStreamType());
                System.out.println("日志：" + new String(item.getPayload()));
                super.onNext(item);
            }
        };

        try {
            // 阻塞等待日志输出
            dockerClient.logContainerCmd(containerId)
                    .withStdErr(true)
                    .withStdOut(true)
                    .exec(logContainerResultCallback)
                    .awaitCompletion();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除容器
     * @param containerId
     * @param dockerClient
     */
    public static void removeContainer(String containerId, DockerClient dockerClient){
        dockerClient.removeContainerCmd(containerId).exec();
    }

    /**
     * 删除镜像
     * @param imageName
     * @param dockerClient
     */
    public static void removeImage(String imageName,DockerClient dockerClient){
        dockerClient.removeImageCmd(imageName).exec();
    }
}
